<?php
namespace Admin\Controller;
/**
* 登录
*/
class LoginController extends CommonController{
	public $exec_comfunc = 'index';
	//登录页面
	public function index(){
		if(IS_POST){
			$username      = $this->_post('username','对不起，用户名不能为空！');
			$password      = $this->_post('password','对不起，密码不能为空！');
			$save_username = $this->_post('save_username');

			$username = trim($username);
			$password = trim($password);

			$where['username'] = $username;
			$info = D('Admin')->where($where)->find();
			
			$password_md5 = password_md5($username,$password);
			//判断密码
			if($info['password'] != $password_md5){
				$this->error('对不起，您输入密码有误！');
			}
			//判断是否禁用
			if($info['status']==0){
				$this->error('对不起，您的账号被禁用，请联系相关人员！');
			}
			
			$time = 24*60*60;
	        session(array('expire'=>$time));
	        session('user_id',$info['id']);
	        //保存用户名
	        if($save_username==1){
	        	cookie('username',$username);
	        }else{
	        	cookie('username',null);
	        }
	        //有回调的跳转到回调地址
	        $return_url = urldecode($this->_get('return_url'));
	        $q = strpos('?', $$return_url) !== false ? '&' : '?';
            $jump_return_url = $return_url.$q.'token='.$user_token;
	        if($return_url){
	        	$this->success('登录成功，正在跳转...',$jump_return_url.'');
	        }
	        //正常登录
	        $this->success('登录成功！',U('Index/index'));
		}
		$this->assign('username',cookie('username'));
		$this->assign('title','登录');
		$this->display();
	}
}
?>